package com.example.app.pages.main.home.me.viewModel

import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateListOf
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.runtime.snapshots.SnapshotStateList
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.example.app.cls.MyState
import com.example.app.components.MyCheckBox.MyCustomCheckBoxItem
import com.example.app.data.bean.BaseResponseData
import com.example.app.data.bean.routeAddAccount.AccountRelationPeople
import com.example.app.network.api.AccountService
import com.example.app.viewmodel.MainViewModel
import kotlinx.coroutines.CoroutineStart
import kotlinx.coroutines.Deferred
import kotlinx.coroutines.Job
import kotlinx.coroutines.async
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.flow.update
import kotlinx.coroutines.joinAll
import kotlinx.coroutines.launch
import retrofit2.HttpException

enum class AccountType(
        var type: Boolean,
        var title: String,
        var typeName: String,
        var index: Int
) {
    INCOME(
        true,
        "收入",
        "INCOME",
        0
    ),
    PAY(
        false,
        "支出",
        "PAY",
        1
    )
}

class AddNewViewModel(var mainViewModel: MainViewModel) : ViewModel() {
    //成员管理的关系人，checkbox，list
    var checkBoxListIncomeRelationPeopleItemList = mutableStateListOf<MyCustomCheckBoxItem>()
    //搜索提示的，checkbox，list
    var checkBoxListIncomeSearchList = mutableStateListOf<MyCustomCheckBoxItem>()
    //收入还是支出
    var _accountType = MutableStateFlow(AccountType.INCOME)
    var accountType = _accountType.asStateFlow()
    fun flowUpdate_accountType(value: AccountType) {
        _accountType.update { value }
    }

    var _accountRelationPeopleList = MutableStateFlow<List<AccountRelationPeople>?>(null)
    var accountRelationPeopleList = _accountRelationPeopleList.asStateFlow()
    fun flowUpdate_accountRelationPeopleList(value: List<AccountRelationPeople>) {
        _accountRelationPeopleList.update {
            value.toMutableList()
        }
    }
    //更新
    fun updateAccountRelationPeopleList() {
        viewModelScope.async {
            var data = request_getAccountRelationPeople().await()
            if (data.code < 1) {
                flowUpdate_accountRelationPeopleList(data.data!!)
                //todo 这里更新没完成
                var newCheckBoxListIncomeRelationPeopleItemList =
                    mutableListOf<MyCustomCheckBoxItem>()

                data.data!!.forEach {
                    newCheckBoxListIncomeRelationPeopleItemList.add(
                        MyCustomCheckBoxItem(
                            false,
                            it
                        )
                    )
                }

                checkBoxListIncomeRelationPeopleItemList.clear()
                checkBoxListIncomeRelationPeopleItemList.addAll(newCheckBoxListIncomeRelationPeopleItemList)
            }
        }
    }
    //获取关系
    fun request_getAccountRelationPeople(): Deferred<BaseResponseData<List<AccountRelationPeople>>> {
        return viewModelScope.async {
            try {
                var result = viewModelScope.async {
                    AccountService.service.accountRelation()
                }
                    .await()

                mainViewModel.requestHandler(result)
                return@async result
            } catch (e: HttpException) {
                var result = viewModelScope.async {
                    BaseResponseData<List<AccountRelationPeople>>(
                        code = e.code(),
                        data = null,
                        msg = e.message.toString()
                    )
                }
                    .await()

                mainViewModel.requestHandler(result)
                return@async result
            }
        }
    }
    //新增关系
    fun request_addAccountRelationPeople(Title: String): Deferred<BaseResponseData<AccountRelationPeople>> {
        return viewModelScope.async {
            try {
                var result = viewModelScope.async {
                    AccountService.service.addAccountRelationPeople(Title = Title)
                }
                    .await()

                mainViewModel.requestHandler(result)
                return@async result
            } catch (e: HttpException) {
                var result = viewModelScope.async {
                    BaseResponseData<AccountRelationPeople>(
                        code = e.code(),
                        data = null,
                        msg = e.message.toString()
                    )
                }
                    .await()

                mainViewModel.requestHandler(result)
                return@async result
            }
        }
    }
    //修改关系
    fun request_editAccountRelationPeople(
            Title: String,
            _id: String
    ): Deferred<BaseResponseData<AccountRelationPeople>> {
        return viewModelScope.async {
            try {
                var result = viewModelScope.async {
                    AccountService.service.updateAccountRelationPeople(
                        Title = Title,
                        _id = _id
                    )
                }
                    .await()

                mainViewModel.requestHandler(result)
                return@async result
            } catch (e: HttpException) {
                var result = viewModelScope.async {
                    BaseResponseData<AccountRelationPeople>(
                        code = e.code(),
                        data = null,
                        msg = e.message.toString()
                    )
                }
                    .await()

                mainViewModel.requestHandler(result)
                return@async result
            }
        }
    }

    fun request_deleteManyAccountRelationPeoples(
            list: MutableList<AccountRelationPeople>
    ): SnapshotStateList<Deferred<BaseResponseData<AccountRelationPeople>>> {
        var deferredArr = mutableStateListOf<Deferred<BaseResponseData<AccountRelationPeople>>>()
        list.forEachIndexed { index, accountRelationPeople ->
            deferredArr.add(
                request_deleteAccountRelationPeople(
                    DefineType = accountRelationPeople.DefineType!!,
                    AccountRelationId = accountRelationPeople._id!!
                )
            )
        }
        return deferredArr
    }
    //删除关系
    fun request_deleteAccountRelationPeople(
            DefineType: String,
            AccountRelationId: String,
    ): Deferred<BaseResponseData<AccountRelationPeople>> {
        return viewModelScope.async {
            try {
                var result = viewModelScope.async {
                    AccountService.service.removeAccountRelationPeople(
                        DefineType = DefineType,
                        AccountRelationId = AccountRelationId
                    )
                }
                    .await()

                mainViewModel.requestHandler(result)
                return@async result
            } catch (e: HttpException) {
                var result = viewModelScope.async {
                    BaseResponseData<AccountRelationPeople>(
                        code = e.code(),
                        data = null,
                        msg = e.message.toString()
                    )
                }
                    .await()

                mainViewModel.requestHandler(result)
                return@async result
            }
        }
    }
}